--
-- Author: shunguo.chen
-- Date: 2018-07-30 14:12:45
--
-- LifeRender
--

local ClassRef = app.mvc.createRenderClass(...)

--data = bool 是否显示
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
    self.super.init(self, data)

    self._timeline = self:getTimeline()
    self._timeline:gotoFrameAndPause(0)
    self._timeline:setLastFrameCallFunc(function()
        self._star:setVisible(false)
        self._light:setVisible(false)
        executeFunc(self._actionOverCB, self)
    end)

    -- self:refresh()
end

function ClassRef:setIsEnemy(b)
    self._ui.bg_1:setVisible(not b)
    self._ui.bg_2:setVisible(b)

    self._star = b and self._ui.bg_2 or self._ui.bg_1
    self._light = b and self._ui.light_2 or self._ui.light_1
end

function ClassRef:setData(data)
    self._preData = self._data

    self.super.setData(self, data)
end

function ClassRef:refresh()
    if self._data then
        self._timeline:gotoFrameAndPause(0)
        self._star:setVisible(true)
        self._light:setVisible(false)
    else
        -- 由显示变成不显示，需要播放一个动画
        if self._preData then
            self._light:setVisible(true)
            self._timeline:gotoFrameAndPlay(0, false)
        else
            self._star:setVisible(false)
        end
    end
end

function ClassRef:onActionOver(cb)
    self._actionOverCB = cb
end

return ClassRef
